<template>
  <el-card>
    <h3>找回密码</h3>
    <div class="nextstep">
      <el-form class="form" :model="form" :rules="rules" ref="form" label-width="110px">
        <el-form-item size="small" prop="oldPasswd" label="密码：">
          <el-input placeholder="" type="password" v-model="form.oldPasswd"></el-input>
        </el-form-item>
        <el-form-item size="small" prop="newPasswd" label="确认密码：">
          <el-input placeholder="" type="password" v-model="form.newPasswd"></el-input>
        </el-form-item>
        <div class="btns">
          <el-button class="setup" @click="gologin" type="primary" size="small">返回登录</el-button>
          <el-button class="setup" @click="sure" type="primary" size="small">确认提交</el-button>
        </div>
      </el-form>
  </div>
  </el-card>
</template>
<script>
export default {
  name: 'nextstep',
  data () {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.oldPasswd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        oldPasswd: '',
        newPasswd: ''
      },
      rules: {
        oldPasswd: [
          { required: true, message: '请填写密码！', trigger: 'blur' },
          { pattern: /^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{8,32}$/, message: '密码8-32位，必须同时大小写+数字组合', trigger: 'blur' }
        ],
        newPasswd: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    console.log(this.$store.state)
  },
  methods: {
    gologin () {
      this.$router.push('/login')
    },
    sure () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let json = {
            userId: this.$store.state.content,
            oldPasswd: '',
            newPasswd: this.$md5(this.form.newPasswd)
          }
          this.$tkAjax.post(`${this.$config.baseUrl}/user/merchantUser/resetMerchantPasswd`, {}, {
            params: json
          }).then(res => {
            this.$message.success(res.data.message)
            this.$router.push('/login')
          }).catch(err => {
            console.log(err)
            this.$message.warning(err.data.message)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scope>
.nextstep {
  width: 40%;
  margin: 0 auto;
  padding: 50px 0;
  .form {
    .getimg {
      width: 123px;
      height: 33px;
      border-radius: 5px;
      background-color: #000;
      margin: 20px 0 20px 112px;
      .img {
        display: block;
        width: 100%;
        height: 100%;
      }
    }
    .btns {
      text-align: center;
      margin: 60px 0 20px;
      .setup {
        border-radius: 4px;
      }
    }
    .phonecode {
      .el-input {
        width: 340px;
      }
      .el-button {
        margin-left: 70px;
      }
    }
  }
}
</style>
